package com.ydd.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ydd.dto.SysRoleQueryDTO;
import com.ydd.mapper.SysRoleMapper;
import com.ydd.mapper.SysRoleMenuMapper;
import com.ydd.service.RoleService;
import com.ydd.vo.DataListVO;
import com.ydd.vo.SysRolePageVO;
import com.ydd.vo.SysRoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImp implements RoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    /**
     * 根据name分页查询
     * @param sysRoleQueryDTO
     * @return
     */
    @Override
    public SysRolePageVO findPageByName(SysRoleQueryDTO sysRoleQueryDTO) {
        PageHelper.startPage(sysRoleQueryDTO.getPage(),sysRoleQueryDTO.getLimit());
        List<SysRoleVO> sysRoleVOList=sysRoleMapper.findPageByName(sysRoleQueryDTO.getName());
        for (SysRoleVO sysRoleVO : sysRoleVOList) {
            sysRoleVO.setRoleId(sysRoleVO.getId());
            List<Integer> MenuIds=sysRoleMenuMapper.findMenuIdByroleId(sysRoleVO.getRoleId());
            sysRoleVO.setMeuids(MenuIds);
        }
        Page<SysRoleVO> pageInfo=(Page<SysRoleVO>) sysRoleVOList;
        Integer count=(int) pageInfo.getTotal();
        return new SysRolePageVO(
                count,
                pageInfo.getPages(),
                sysRoleQueryDTO.getLimit(),
                sysRoleQueryDTO.getPage(),
                pageInfo.getResult()
        );
    }
}
